#include<iostream>
#include<stdio.h>
#include<unistd.h>
#include<sys/wait.h>
#include<sys/types.h>
#include<errno.h>
#include<string.h>
#include<stdlib.h>
using namespace std;

int main()
{
  pid_t id = fork();
  if(id == 0)//child
  {
    int count=5;
    while(count--)
    {
      cout<<"我是子进程"<<",我的pid是"<<getpid()<<endl;
      sleep(1);
    }
    exit(1);
  }
  while(true)
  {
    int status = 0;
    pid_t rid =waitpid(-1,&status,WNOHANG);
    if(rid>0)
    {
      cout<<"wait success"<<endl;
      break;
    }
    else if(rid<0)
    {
      cout<<"wait fail"<<endl;
      cout<<"exit code:"<<((status>>8)&0xFF)<<" exit Signal:"<<(status&0x7F);
      break;
    }
    else//parent do 
    {
      cout<<"我是父进程，我的pid是"<<getpid()<<endl;
      sleep(1);
    }
  }
  return 0;
}
